package com.cqut.forum.dao.imp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.cqut.forum.dao.UserNewsDao;
import com.cqut.forum.entity.UserNews;

@Component
public class UserNewsDaoImp extends BaseDaoImp implements UserNewsDao{
	
	public boolean unDelete(int id) {
		return false;
	}

	@SuppressWarnings("unchecked")
	public int getCareUnreadNotice(int userId) {
		String sql1 = "select count(*) from Review r,ArticleReply ar where " +
				"(r.reviewState=true and r.picture.ownerId in " +
				"(select r.friendId from Relation r where r.user.userId=?)) " +
				"or (ar.articleReplyState=true and ar.article.ownerId in " +
				"(select r.friendId from Relation r where r.user.userId=?))";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		parameters.add(userId);
		
		List<Long> result = (List<Long>) super.excuteHql(sql1, parameters, null);
		return (int)(long)result.get(0);
	}

	/*@SuppressWarnings("unchecked")
	public int getFriendUnreadNotice(int userId) {
		String sql = "select count(*) from Article a,Picture p where a.articleGroup.ownerId " +
				"in " +
				"(select from Relation r where r.user.userId = ? and r.isCare = ?)";
		String sql = "select count(*) from Article a,Relation r where a.articleGroup.ownerId " +
				"=r.friendId and r.user.userId = ? and r.isCare = ? "; 
		String hql = "select count(*) from UserNews un,Relation r where " +
		"un.moduleName = ? and un.newsState = ?";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		parameters.add(false);
		
		List<Integer> result = (List<Integer>) super.excuteHql(hql, parameters, null);
		return result.get(0);
	}*/
	
	@SuppressWarnings("unchecked")
	public int getArticleUnreadNotice(int userId,int moduleId) {
		String sql = "select count(*) from UserNews un where un.acceptUserId = ? " +
		"and un.moduleId = ? and un.userNewsState = ?";
		List<Object> parameters = new ArrayList();
		parameters.add(userId);
		parameters.add(moduleId);
		parameters.add(true);
		
		List<Long> result = (List<Long>) super.excuteHql(sql, parameters, null);
		return (int)(long)result.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public int getDiscussUnreadNotice(int userId,int moduleId) {
		String sql = "select count(*) from UserNews un where un.acceptUserId = ? " +
		"and un.moduleId = ? and un.userNewsState = ?";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		parameters.add(moduleId);
		parameters.add(true);
		
		List<Long> result = (List<Long>) super.excuteHql(sql, parameters, null);
		return (int)(long)result.get(0);
	}

	@SuppressWarnings("unchecked")
	public int getPictureUnreadNotice(int userId) {
		String hql = "select count(r.reviewId) from Review r where " +
				"r.picture.ownerId=? and r.reviewState=?";
		List<Object> parameters = new ArrayList();
		parameters.add(userId);
		//parameters.add("图片");
		parameters.add(true);
		
		List<Long> result = (List<Long>) super.excuteHql(hql, parameters, null);
		return (int)(long)result.get(0);
	}

	public List<?> getVisitMe(int userId) {
		String sql = "select u.user_id,u.user_photo,u.user_name,un.news_time from user_news un,user u where un.accept_user_id = ? " +
				"and u.user_id=un.accept_user_id";
		Query query = super.getSqlQuery(sql);
		query.setInteger(0, userId);
		
		List<?> result = query.list();
		
		return result;
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getVisitOther(int userId,int index, int size) {
		String sql = "select u.user_id userId,u.user_name userName,u.user_photo userPhoto," +
					 "un.news_time newsTime from user u,user_news un " +
					 "un.user_id=? and un.accept_user_id=u.user_id and u.user_state=true " +
					 "and un.user_news_state=true order by un.news_time desc";

		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		
		Map<String, Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		
		List<Map<String, Object>> result = (List<Map<String, Object>>)super.excuteSql(sql, parameters, pages);
		return result;
	}
	/*@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getMessages(int userId,int moduleId, int index, int size) {
		String sql = "select un.news_contents contents,un.user_news_id messageId,u.user_id userId,u.user_name userName," +
					 "u.user_photo userPhoto,un.news_time newsTime from user_news un,user u " +
					 "where un.user_id=u.user_id and " +
					 "un.accept_user_id=? and un.module_id=? order by un.news_time desc";
		
		String sql1 = "select u.user_id,u.user_photo,u.user_name,un.user_news_id,un.news_contents,un.news_time " +
					 "from user u,user_news un where un.module_id=? and un.user_id=u.user_id and un.accept_user_id=? " +
					 "un.previous_news_id = un.user_news_id order by un.news_time desc";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(moduleId);
		parameters.add(userId);
		
		Map<String, Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		
		List<Map<String, Object>> result = (List<Map<String, Object>>)super.excuteSql(sql1, parameters, pages);
		return result;
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getMessageReply(List<Integer> articleIds){
		String sql = "select u.user_id,u.user_photo,u.user_name,un.news_contents,un.news_time,un.user_news_id " +
					 "from user u,user_news un " +
					 "where un.user_id = u.user_id and un.previous_news_id in (";

		List<Object> parameters = new ArrayList<Object>();
		for(Integer i : articleIds){
			sql = sql + "?,";
			parameters.add(i);
		}
		sql = sql.substring(0,sql.length()-1)+") order by un.news_time";
		//调用父类中的方法获取数据
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters, null);
		
		return result;
	}*/
	@SuppressWarnings("unchecked")
	@Override
	public UserNews isVisited(int visitor, int userId) {
		Date d = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String now = format.format(d);
		String hql = "select un from UserNews un where un.user.userId=? and un.acceptUserId=? " +
					 "and un.newsTime > '"+now+"'";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(visitor);
		parameters.add(userId);
		
		List<UserNews> result = (List<UserNews>)super.excuteHql(hql, parameters, null);
		return result.size()==0?null:result.get(0);
	}

	/*@Override
	public boolean haveReadMyMessages(int userId, int moduleId) {
		String sql = "update user_news un set un.user_news_state=false where un.accept_user_id=? " +
					 "and un.module_id=?";
		Query query = super.getSqlQuery(sql);
		query.setInteger(0, userId);
		query.setInteger(1, moduleId);
		query.executeUpdate();
		return true;
	}*/
}
